/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pa165.dominatingspecies.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import pa165.dominating.species.service.AnimalService;
import pa165.dominating.species.dao.AnimalDAO;
import pa165.dominating.species.entities.Animal;

import pa165.dominatingspecies.service.converter.AnimalConverter;
import pa165.dominatingspecies.service.converter.EnvironmentConverter;
import pa165.dominating.species.tos.AnimalTo;
import pa165.dominating.species.tos.EnvironmentTo;

/**
 *
 * @author Filip Dinh
 */
public class AnimalServiceImpl implements AnimalService {

    @Autowired
    private AnimalDAO animalDao;
    @Autowired
    private AnimalConverter animalConverter;
    @Autowired
    private EnvironmentConverter environmentConverter;

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @PreAuthorize("hasRole('ADMIN')")
    public void createAnimal(AnimalTo animalTo) {
        Animal animal = animalConverter.convertAnimalTo2Animal(animalTo);
        animalDao.createAnimal(animal);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @PreAuthorize("hasRole('WORKER')")
    public void updateAnimal(AnimalTo animalTo) {
        Animal animal = animalConverter.convertAnimalTo2Animal(animalTo);
        animalDao.updateAnimal(animal);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @PreAuthorize("hasRole('ADMIN')")
    public void deleteAnimal(AnimalTo animalTo) {
        Animal animal = animalConverter.convertAnimalTo2Animal(animalTo);
        animalDao.deleteAnimal(animal);
    }

    @Override
    @PreAuthorize("hasRole('WORKER')")
    public List<AnimalTo> getAllAnimals() {
        List<Animal> animalList = animalDao.getAllAnimals();
        List<AnimalTo> animalToList = new ArrayList<>();
        for (Animal a : animalList) {
            animalToList.add(animalConverter.convertAnimal2AnimalTo(a));
        }
        return animalToList;
    }

    @Override
    @PreAuthorize("hasRole('WORKER')")
    public AnimalTo getAnimalById(Long id) {
        Animal animal = animalDao.getAnimalById(id);
        return animalConverter.convertAnimal2AnimalTo(animal);
    }

    @Override
    @PreAuthorize("hasRole('WORKER')")
    public List<AnimalTo> getAnimalsByEnvironment(EnvironmentTo environmentTo) {
        List<Animal> animalList = animalDao.getAnimalsByEnvironment(environmentConverter.convertEnvironmentTo2Environment(environmentTo));
        List<AnimalTo> animalToList = new ArrayList<>();
        for (Animal a : animalList) {
            animalToList.add(animalConverter.convertAnimal2AnimalTo(a));
        }
        return animalToList;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @PreAuthorize("hasRole('WORKER')")
    public void addPreyToPredator(AnimalTo predator, AnimalTo prey) {
        animalDao.addPreyToPredator(animalConverter.convertAnimalTo2Animal(predator), animalConverter.convertAnimalTo2Animal(prey));
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @PreAuthorize("hasRole('WORKER')")
    public void removePreyOfPredator(AnimalTo predator, AnimalTo prey) {
        animalDao.removePreyOfPredator(animalConverter.convertAnimalTo2Animal(predator), animalConverter.convertAnimalTo2Animal(prey));
    }

    @Override
    @PreAuthorize("hasRole('WORKER')")
    public List<AnimalTo> getPredatorsOfPrey(AnimalTo prey) {
        List<Animal> animalList = animalDao.getPredatorsOfPrey(animalConverter.convertAnimalTo2Animal(prey));
        List<AnimalTo> animalToList = new ArrayList<>();
        for (Animal a : animalList) {
            animalToList.add(animalConverter.convertAnimal2AnimalTo(a));
        }
        return animalToList;
    }

    @Override
    @PreAuthorize("hasRole('WORKER')")
    public List<AnimalTo> getPreyOfPredator(AnimalTo predator) {
        List<Animal> animalList = animalDao.getPreyOfPredator(animalConverter.convertAnimalTo2Animal(predator));
        List<AnimalTo> animalToList = new ArrayList<>();
        for (Animal a : animalList) {
            animalToList.add(animalConverter.convertAnimal2AnimalTo(a));
        }
        return animalToList;
    }

    public void setAnimalDao(AnimalDAO animalDao) {
        this.animalDao = animalDao;
    }

    public void setAnimalConverter(AnimalConverter animalConverter) {
        this.animalConverter = animalConverter;
    }

    public void setEnvironmentConverter(EnvironmentConverter environmentConverter) {
        this.environmentConverter = environmentConverter;
    }

    public AnimalDAO getAnimalDao() {
        return animalDao;
    }

    public AnimalConverter getAnimalConverter() {
        return animalConverter;
    }

    public EnvironmentConverter getEnvironmentConverter() {
        return environmentConverter;
    }
}
